// 用于存放不同的 http 错误类型

// http异常
export class HttpException extends Error {
  public message: string
  public errorCode: number
  public code: number
  public data: any
  public isBuffer = false
  public responseType: string | undefined
  constructor(
    { data = {}, msg = '服务器异常, 请联系管理员', errorCode = 1000, code = 501 } = {
      data: null,
      msg: '服务器异常, 请联系管理员',
      errorCode: 1000,
      code: 501,
    } as any,
  ) {
    super()
    this.message = msg
    this.errorCode = errorCode
    this.code = code
    this.data = data
  }
}
export class ImportException extends HttpException {
  constructor(
    { data = null, msg = '文件内容解析错误', errorCode = 424 } = {
      data: null,
      msg: '文件内容解析错误',
      errorCode: 424,
    } as any,
  ) {
    super()
    this.code = 200
    this.message = msg
    this.errorCode = errorCode
    this.data = data
  }
}
// http参数异常
export class ParameterException extends HttpException {
  constructor(
    { msg = '参数错误', errorCode = 10000 } = {
      msg: '参数错误',
      errorCode: 10000,
    } as any,
  ) {
    super()
    this.code = 422
    this.message = msg
    this.errorCode = errorCode
  }
}
// http请求成功
export class Success extends HttpException {
  public data
  public total
  public responseType
  public session
  constructor(
    { data = null, total = 0, msg = 'ok', code = 200, errorCode = 0, responseType = '', session = '' } = {
      data: null,
      total: 0,
      msg: 'ok',
      code: 200,
      errorCode: 0,
      responseType: '',
      session: '',
    } as any,
  ) {
    super()
    this.code = code //200查询成功, 201操作成功
    this.message = msg
    this.errorCode = errorCode || 0
    this.data = data
    this.total = total
    this.responseType = responseType
    this.session = session
  }
}

// 返回文件流

export class Buffer extends Success {
  public data
  public responseType
  public session
  public isBuffer
  constructor(
    { data = null, responseType = 'blob', session = '' } = {
      data: null,
      responseType: 'blob',
      session: '',
    } as any,
  ) {
    super()
    this.code = 200 //200查询成功, 201操作成功
    this.message = 'ok'
    this.errorCode = 0
    this.data = data
    this.responseType = responseType
    this.session = session
    this.isBuffer = true
  }
}

// // 404
export class NotFount extends HttpException {
  constructor({ msg = '资源未找到', errorCode = 0 } = { msg: '资源未找到', errorCode: 10001 } as any) {
    super()
    this.code = 404
    this.message = msg
    this.errorCode = errorCode
  }
}

// 授权失败
export class AuthFailed extends HttpException {
  constructor({ msg = '授权失败', errorCode = 10002 } = { msg: '授权失败', errorCode: 10002 } as any) {
    super()
    this.code = 401
    this.message = msg
    this.errorCode = errorCode
  }
}
// Forbidden
export class Forbidden extends HttpException {
  constructor({ msg = '禁止访问', errorCode = 100006 } = { msg: '禁止访问', errorCode: 100006 } as any) {
    super()
    this.code = 403
    this.message = msg
    this.errorCode = errorCode
  }
}
// 查询失败
export class QueryFailed extends HttpException {
  constructor(
    { msg = '未查到匹配的数据', errorCode = 100006 } = { msg: '未查到匹配的数据', errorCode: 100006 } as any,
  ) {
    super()
    this.code = 500
    this.message = msg
    this.errorCode = errorCode
  }
}
// 查询失败
export class DataBaseFailed extends HttpException {
  constructor({ msg = '数据库出错', errorCode = 100005 } = { msg: '数据库出错', errorCode: 10005 } as any) {
    super()
    this.code = 500
    this.message = msg
    this.errorCode = errorCode
  }
}
